<template>
  <div class="banner-container">
    <div class="query-content">
      <el-form
        ref="queryForm"
        class="queryform"
        :inline="true"
        label-width="100px"
        :model="queryForm"
        size="small"
        @submit.native.prevent
      >
        <el-row :gutter="20">
          <el-col :span="18">
            <el-col :span="24">
              <el-col v-bind="grid">
                <el-form-item label="名称:">
                  <el-input
                    v-model.trim="queryForm.name"
                    clearable
                    placeholder="名称"
                  />
                </el-form-item>
              </el-col>
              <el-col v-bind="grid">
                <el-form-item label="用户手机:">
                  <el-input
                    v-model.trim="queryForm.mobile"
                    clearable
                    placeholder="请输入用户姓名"
                  />
                </el-form-item>
              </el-col>
              <el-col v-bind="grid">
                <el-form-item label="设备:">
                  <el-select
                    v-model="queryForm.device_id"
                    clearable
                    placeholder="请选择品牌"
                  >
                    <el-option
                      v-for="item in deviceList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-col>
          </el-col>
          <el-col class="query-search" :span="6">
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
            <el-button @click="resetForm('queryForm')">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
        width="55"
      />
      <el-table-column
        align="center"
        label="名称"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户"
        prop="username"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="设备"
        prop="device_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="mac地址"
        prop="mac_address"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作时间"
        prop="created_at"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column align="center" label="操作" width="140">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/userDevice'
  import { getList as getDeviceList } from '@/api/device'
  import Edit from './components/Edit'
  export default {
    name: 'UserDevice',
    components: { Edit },
    data() {
      return {
        grid: {
          xl: 8,
          lg: 8,
          md: 12,
          sm: 24,
          xs: 24,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          name: '',
          mobile: '',
          device_id: '',
          pageNo: 1,
          pageSize: 10,
        },
        deviceList: [],
      }
    },
    created() {
      this.fetchData()
      this.fetchDeviceData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { message } = await doDelete({ ids: row.id })
            this.$baseMessage(message, 'success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { message } = await doDelete({ ids })
              this.$baseMessage(message, 'success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      resetForm() {
        this.queryForm = {
          name: '',
          mobile: '',
          device_id: '',
          pageNo: 1,
          pageSize: 10,
        }
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        const { list, total } = data
        this.list = list
        this.total = total
        this.listLoading = false
      },
      async fetchDeviceData() {
        const { data } = await getDeviceList({ pageSize: 0 })
        const { list } = data
        this.deviceList = list
      },
      onCopy() {
        this.$baseMessage('复制成功', 'success')
      },
      onCopyError() {
        this.$baseMessage('复制事变', 'error')
      },
    },
  }
</script>
